package com.story.api.moduler.song.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.story.api.common.persistence.dao.ChannelMapper;
import com.story.api.common.persistence.dao.UserChannelMapper;
import com.story.api.common.persistence.model.Channel;
import com.story.api.common.persistence.model.UserChannel;
import com.story.api.common.result.ServerResponse;
import com.story.api.config.properties.JwtProperties;
import com.story.api.moduler.song.dao.UserChannelDao;
import com.story.api.moduler.song.service.ChannelService;
import com.story.api.moduler.user.util.JwtTokenUtil;
import com.story.core.base.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 频道管理
 * @author yangbo
 * @date 2019-08-08 11:55
 */
@RestController
@RequestMapping(value = "/child")
public class ChannelController extends BaseController {

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private UserChannelDao userChannelDao;

    @Autowired
    private ChannelService channelService;

    /**
     * 频道列表
     * @return
     */
    @GetMapping(value = "/channel/list")
    public ServerResponse<List<Channel>> channelList() {
        List<Channel> channelList = channelMapper.selectList(new EntityWrapper<>());
        return ServerResponse.getSuccess("success",channelList);
    }

    /**
     * 用户收藏的频道
     * @return
     */
    @GetMapping(value = "/channel/user/list")
    public ServerResponse<List<Channel>> userChannelList() {
        Integer userId = getUserId();
        if(userId == null) {
            return ServerResponse.getErrorMessage("用户token已失效");
        }
        List<Channel> channelList = userChannelDao.userChannelList(userId);
        return ServerResponse.getSuccess("success",channelList);
    }

    /**
     * 用户收藏频道
     * @param channelIds
     * @return
     */
    @PostMapping(value = "/channel/user/bind")
    public ServerResponse userBindChannel(@RequestBody List<Integer> channelIds) {
        Integer userId = getUserId();
        if(userId == null) {
            return ServerResponse.getErrorMessage("用户token已失效");
        }
        for (Integer id : channelIds) {
            if(id <= 0) {
                return ServerResponse.getErrorMessage("参数错误");
            }

        }
        channelService.bindUserAndChannel(userId,channelIds);
        return ServerResponse.getSuccess("关注成功");
    }

    /**
     * 解绑关系
     * @param channelIds
     * @return
     */
    @PostMapping(value = "/channel/user/unbind")
    public ServerResponse userUnbindChannel(@RequestBody List<Integer> channelIds) {
        Integer userId = getUserId();
        if(userId == null) {
            return ServerResponse.getErrorMessage("用户token已失效");
        }
        for (Integer id : channelIds) {
            if(id <= 0) {
                return ServerResponse.getErrorMessage("参数错误");
            }

        }
        channelService.unbindUserAndChannel(userId,channelIds);
        return ServerResponse.getSuccess("取消关注成功");
    }

    /**
     * 频道下的媒体资源列表
     * @param channelId
     * @return
     */
    @GetMapping(value = "/channel/radio/list")
    public ServerResponse channelResources(@RequestParam(value = "channelId") Integer channelId) {
        return ServerResponse.getSuccess("success",channelService.channelResources(channelId));
    }

    /**
     * 通过jwt得到userId
     * @return
     */
    private Integer getUserId() {
        HttpServletRequest request = super.getHttpServletRequest();
        final String requestHeader = request.getHeader(jwtProperties.getHeader());
        String authToken;
        if (StringUtils.isNotBlank(requestHeader) && requestHeader.startsWith("Bearer ")) {
            authToken = requestHeader.substring(7);
            Integer userId = Integer.valueOf(jwtTokenUtil.getUsernameFromToken(authToken));
            return userId;
        } else {
            return null;
        }
    }
}
